package com.leyou.item.web;

import com.leyou.item.dto.SpecGroupDTO;
import com.leyou.item.dto.SpecParamDTO;
import com.leyou.item.entity.SpecGroup;
import com.leyou.item.entity.SpecParam;
import com.leyou.item.service.SpecGroupService;
import com.leyou.item.service.SpecParamService;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/spec")
public class SpecController {
    @Resource
    private SpecGroupService specGroupService;

    @Resource
    private SpecParamService specParamService;

    /**
     * 根据商品分类id，查询规格组的集合
     *
     * @param categoryId 商品分类id
     * @return 规格组的集合
     */
    @GetMapping("/groups/of/category")
    public ResponseEntity<List<SpecGroupDTO>> querySpecGroupByCategoryId(@RequestParam("id") Long categoryId) {
        return ResponseEntity.ok(specGroupService.querySpecGroupByCategoryId(categoryId));
    }

    /**
     * 根据分类id查询规格组及组内参数
     *
     * @param categoryId 分类id
     * @return 规格组及组内参数
     */
    @GetMapping("/list")
    public ResponseEntity<List<SpecGroupDTO>> querySpecDetailByCategoryId(@RequestParam("id") Long categoryId) {
        return ResponseEntity.ok(specGroupService.querySpecDetailByCategoryId(categoryId));
    }

    /**
     * 新增规格租
     * @param specGroupDto  表单数据
     * @return  无数据
     */
    @PostMapping("/group")
    public ResponseEntity<Void> saveGroup(@RequestBody SpecGroupDTO specGroupDto){
        SpecGroup specGroup = specGroupDto.toEntity(SpecGroup.class);
        specGroupService.save(specGroup);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    /**
     * 修改规格组
     * @param specGroupDto      需要修改的数据
     * @return
     */
    @PutMapping("/group")
    public ResponseEntity<Void> updateGroup(@RequestBody SpecGroupDTO specGroupDto){
        SpecGroup specGroup = specGroupDto.toEntity(SpecGroup.class);
        specGroupService.updateById(specGroup);
        return ResponseEntity.ok().build();
    }

    /**
     * 删除规格组
     * @param id
     * @return
     */
    @DeleteMapping("/group/{id}")
    public ResponseEntity<Void> deleteGroup(@PathVariable("id") Long id){
        specGroupService.removeById(id);
        return ResponseEntity.ok().build();
    }

    /**
     * 根据条件查询规格参数集合
     * @param categoryId        商品分类id
     * @param groupId           规格组id
     * @param searching         是否搜索
     * @return
     */
    @GetMapping("/params")
    public ResponseEntity<List<SpecParamDTO>> querySpecParam(
            @RequestParam(value = "categoryId", required = false) Long categoryId,
            @RequestParam(value = "groupId", required = false) Long groupId,
            @RequestParam(value = "searching", required = false) Boolean searching
    ) {
        return ResponseEntity.ok(specParamService.querySpecParam(categoryId, groupId, searching));
    }

    /**
     * 新增规格参数
     * @param specParamDto    规格参数
     * @return
     */
    @PostMapping("/param")
    public ResponseEntity<Void> saveParam(@RequestBody SpecParamDTO specParamDto){
        SpecParam specParam = specParamDto.toEntity(SpecParam.class);
        specParamService.save(specParam);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    /**
     * 修改规格参数
     * @param specParamDto      规格参数
     * @return
     */
    @PutMapping("/param")
    public ResponseEntity<Void> updateParam(@RequestBody SpecParamDTO specParamDto){
        SpecParam specParam = specParamDto.toEntity(SpecParam.class);
        specParamService.updateById(specParam);
        return ResponseEntity.ok().build();
    }

    /**
     * 删除规格参数
     * @param id
     * @return
     */
    @DeleteMapping("/param/{id}")
    public ResponseEntity<Void> deleteParam(@PathVariable("id") Long id){
        specParamService.removeById(id);
        return ResponseEntity.ok().build();
    }
}